from sqlalchemy import Column, Integer, String, Text, Float, ForeignKey, DateTime, UniqueConstraint
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship
from datetime import datetime

Base = declarative_base()
class User(Base):
    __tablename__ = 'UserTable'
    
    id = Column(Integer, primary_key=True)
    name = Column(String)
    wechat_id = Column(String, nullable=False)
    phone_number = Column(String)
    age = Column(Integer)
    profession = Column(String)
    city = Column(String)
    interests = Column(Text)
    points = Column(Integer, default=0)
    is_auth = Column(Integer, default=0)
    photo_url = Column(String)
    mask_info = Column(Text)
    is_delete = Column(Integer, default=0)
    c_time = Column(DateTime, default=datetime.now)
    u_time = Column(DateTime, default=datetime.now, onupdate=datetime.now)

   # Add relationships
    activities = relationship("Activity", back_populates="creator")
    favorites = relationship("UserFav", back_populates="user")
    comments = relationship("ActivityComment", back_populates="user", foreign_keys="[ActivityComment.user_id]")

class Activity(Base):
    __tablename__ = 'ActivityTable'
    id = Column(Integer, primary_key=True)
    images = Column(Text, nullable=False)
    name = Column(String, nullable=False)
    content = Column(Text, nullable=False)
    type = Column(String, nullable=False)
    address_id = Column(Integer, ForeignKey('AddressTable.id'), nullable=False, unique=True)
    people_number = Column(String, nullable=False)
    people_age = Column(String)
    fee = Column(String, nullable=False)
    start_time = Column(String, nullable=False)
    end_time = Column(String, nullable=False)
    is_delete = Column(Integer, default=0)
    created_user_id = Column(Integer, ForeignKey('UserTable.id'), nullable=False)
    joiners_ids = Column(Text, default='')
    status = Column(Integer, default=0)
    c_time = Column(DateTime, default=datetime.now)
    u_time = Column(DateTime, default=datetime.now, onupdate=datetime.now)

    # Add relationships
    creator = relationship("User", back_populates="activities")
    address = relationship("Address", back_populates="activity", foreign_keys=[address_id])
    favorites = relationship("UserFav", back_populates="activity")
    comments = relationship("ActivityComment", back_populates="activity")

class Address(Base):
    __tablename__ = 'AddressTable'
    id = Column(Integer, primary_key=True)
    address = Column(Text, nullable=False)
    name = Column(String, nullable=False)
    longitude = Column(Float, nullable=False)
    latitude = Column(Float, nullable=False)
    c_time = Column(DateTime, default=datetime.now)
    u_time = Column(DateTime, default=datetime.now, onupdate=datetime.now)

    # Add relationship
    activity = relationship("Activity", back_populates="address", uselist=False, foreign_keys=[Activity.address_id])

class UserReg(Base):
    __tablename__ = 'UserRegTable'
    id = Column(Integer, primary_key=True)
    user_id = Column(Integer, ForeignKey('UserTable.id'), nullable=False)
    activity_id = Column(Integer, ForeignKey('ActivityTable.id'), nullable=False)
    status = Column(Integer, default=0)
    is_delete = Column(Integer, default=0)
    c_time = Column(DateTime, default=datetime.now)
    u_time = Column(DateTime, default=datetime.now, onupdate=datetime.now)
    __table_args__ = (UniqueConstraint('user_id', 'activity_id'),)

class UserFav(Base):
    __tablename__ = 'UserFavTable'
    id = Column(Integer, primary_key=True)
    user_id = Column(Integer, ForeignKey('UserTable.id'), nullable=False)
    activity_id = Column(Integer, ForeignKey('ActivityTable.id'), nullable=False)
    is_delete = Column(Integer, default=0)
    c_time = Column(DateTime, default=datetime.now)
    u_time = Column(DateTime, default=datetime.now, onupdate=datetime.now)
    __table_args__ = (UniqueConstraint('user_id', 'activity_id'),)

    # Add relationships
    user = relationship("User", back_populates="favorites")
    activity = relationship("Activity", back_populates="favorites")

class ActivityStatusFlow(Base):
    __tablename__ = 'ActivityStatusFlowTable'
    id = Column(Integer, primary_key=True)
    activity_id = Column(Integer, ForeignKey('ActivityTable.id'), nullable=False)
    from_status = Column(Integer, nullable=False)
    to_status = Column(Integer, nullable=False)
    change_reason = Column(Text)
    operator_id = Column(Integer, ForeignKey('UserTable.id'), nullable=False)
    c_time = Column(DateTime, default=datetime.now)

class ActivityLog(Base):
    __tablename__ = 'ActivityLogTable'
    id = Column(Integer, primary_key=True)
    activity_id = Column(Integer, ForeignKey('ActivityTable.id'), nullable=False)
    operator_id = Column(Integer, ForeignKey('UserTable.id'), nullable=False)
    operation_type = Column(Integer, nullable=False)
    operation_content = Column(Text, nullable=False)
    old_value = Column(Text)
    new_value = Column(Text)
    c_time = Column(DateTime, default=datetime.now)

class ActivityComment(Base):
    __tablename__ = 'ActivityCommentTable'
    id = Column(Integer, primary_key=True)
    activity_id = Column(Integer, ForeignKey('ActivityTable.id'), nullable=False)
    user_id = Column(Integer, ForeignKey('UserTable.id'), nullable=False)
    image = Column(Text)  # 新增图片字段
    content = Column(Text)  # 修改为可空
    parent_id = Column(Integer, default=0)  # 父评论ID，0表示一级评论
    reply_to_user_id = Column(Integer, ForeignKey('UserTable.id'))  # 回复给谁，用于二级评论
    like_count = Column(Integer, default=0)  # 点赞数
    is_delete = Column(Integer, default=0)
    c_time = Column(DateTime, default=datetime.now)
    u_time = Column(DateTime, default=datetime.now, onupdate=datetime.now)

     # Add relationships
    user = relationship("User", back_populates="comments", foreign_keys=[user_id])
    activity = relationship("Activity", back_populates="comments")

class CommentLike(Base):
    __tablename__ = 'CommentLikeTable'
    id = Column(Integer, primary_key=True)
    comment_id = Column(Integer, ForeignKey('ActivityCommentTable.id'), nullable=False)
    user_id = Column(Integer, ForeignKey('UserTable.id'), nullable=False)
    is_delete = Column(Integer, default=0)
    c_time = Column(DateTime, default=datetime.now)
    __table_args__ = (UniqueConstraint('comment_id', 'user_id'),)

class ActivityChat(Base):
    __tablename__ = 'ActivityChatTable'
    id = Column(Integer, primary_key=True)
    activity_id = Column(Integer, ForeignKey('ActivityTable.id'), nullable=False, unique=True)
    chat_id = Column(String, nullable=False)
    name = Column(String, nullable=False)
    notice = Column(Text)
    is_delete = Column(Integer, default=0)
    c_time = Column(DateTime, default=datetime.now)
    u_time = Column(DateTime, default=datetime.now, onupdate=datetime.now)

class Message(Base):
    __tablename__ = 'MessageTable'
    
    id = Column(Integer, primary_key=True)
    type = Column(Integer, nullable=False, index=True)  # 消息类型：1-活动消息，2-互动消息，3-系统消息
    title = Column(String, nullable=False)  # 消息标题
    content = Column(Text, nullable=False)  # 消息内容
    sender_id = Column(Integer, ForeignKey('UserTable.id'))  # 发送者ID，系统消息可为空
    receiver_id = Column(Integer, ForeignKey('UserTable.id'), nullable=False, index=True)  # 接收者ID
    activity_id = Column(Integer, ForeignKey('ActivityTable.id'))  # 相关活动ID，可为空
    comment_id = Column(Integer, ForeignKey('ActivityCommentTable.id'))  # 相关评论ID，可为空
    status = Column(Integer, default=0, index=True)  # 消息状态：0未读，1已读
    created_at = Column(DateTime, default=datetime.now)  # 创建时间
    updated_at = Column(DateTime, default=datetime.now, onupdate=datetime.now, index=True)  # 更新时间
    
    # 添加关联关系
    sender = relationship("User", foreign_keys=[sender_id], backref="sent_messages")
    receiver = relationship("User", foreign_keys=[receiver_id], backref="received_messages")
    activity = relationship("Activity", backref="messages")
    comment = relationship("ActivityComment", backref="messages")

class UserAuth(Base):
    __tablename__ = 'UserAuthTable'
    
    id = Column(Integer, primary_key=True)
    user_id = Column(Integer, ForeignKey('UserTable.id'), nullable=False)
    real_name = Column(String, nullable=False)
    id_number = Column(String, nullable=False)
    id_front_url = Column(String, nullable=False)
    id_back_url = Column(String, nullable=False)
    auth_status = Column(Integer, default=0)  # 0-待审核，1-已通过，2-未通过
    remark = Column(Text)  # 备注信息（如审核不通过原因）
    create_time = Column(DateTime, default=datetime.now)
    update_time = Column(DateTime, default=datetime.now, onupdate=datetime.now)

    # 添加与 User 表的关联关系
    user = relationship("User", backref="auth_info")